package com.jp.knowledge.logic;

import android.content.Context;

import com.google.gson.JsonObject;
import com.jiayen.cache.FileCacheManager;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.PositionKnowlegeMode;
import com.jp.knowledge.util.ResultSubscriber;

import java.util.List;

/**
 * Created by jiayen on 17/6/9.
 */
public class PositionKnowlegeDataLogic {
    public static  PositionKnowlegeDataLogic instance;

    private static Context mContext;

    private static List<PositionKnowlegeMode>  models;
    private FileCacheManager fileCacheManager;
    public  SucdessCallkBack  callkBack;
    private PositionKnowlegeDataLogic(){
        fileCacheManager = new FileCacheManager();
    }

    public static List<PositionKnowlegeMode> getModels() {
        return models;
    }

    public static void setModels(List<PositionKnowlegeMode> models) {
        PositionKnowlegeDataLogic.models = models;
    }

    public void setCallkBack(SucdessCallkBack callkBack) {
        this.callkBack = callkBack;
    }

    public static PositionKnowlegeDataLogic getInstance(Context context){
       mContext=context;
        return  instance==null?new PositionKnowlegeDataLogic():instance;

    }

    public String getCachePath(){
        return mContext.getCacheDir() + "/skillCache_position_knowlege_all"  + ".dat";
    }
    private void saveCache(final Object data){
        new Thread(new Runnable() {
            @Override
            public void run() {
                fileCacheManager.wirteObject(data, getCachePath());
            }
        }).start();
    }
    public void getKnowlegeData(){
        JsonObject param = new JsonObject();
        HttpHelper.getInstance(mContext).getSkill(param, 01, new ResultSubscriber.OnResultListener() {
            @Override
            public void onStart(int requestType) {}
            @Override
            public void onCompleted(int requestType) {}
            @Override
            public void onError(int requestType) {

                onCompleted(requestType);
            }
            @Override
            public void onNext(IModel data, int requestType) {
                if(data.getErrcode() != 0){
                    ToasUtil.toast(mContext, data.getMessage());
                    onError(requestType);
                } else {
                    models = data.getList(PositionKnowlegeMode.class);
                    if(models!=null){
                        saveCache(models);
                    }
                    if(callkBack!=null){
                        callkBack.done(models);
                    }
                }
            }
        });
    }


    public interface    SucdessCallkBack{

        void done(List<PositionKnowlegeMode>  models);
    }
}
